require('@/public.js');
require('./css/commissionList.css');
require('@/assets/css/drag-up.scss');
import { getWindowWidthAndHeight } from '@/utils/utils';

Jsborya.ready(function () {


	var app = new Vue({
		el: '#app',
		data: {
			off: {
				type: 1,
				empty: true,
			},
			isPullLoad: true,//上拉加载开关
			isHaveMore: 0,//是否还有更多号码
			lastId: 0,//当前页数
			boxHeight: '400px',
			totalFee: 0,
			monthList: [],

		},
		created: function () {
			Jsborya.webviewLoading({ isLoad: false });//关闭app加载层
		},
		mounted: function () {
			const vm = this;

			// 获取窗口宽高
			let { windowHeight } = getWindowWidthAndHeight();
			vm.boxHeight = windowHeight - 122 + 'px';

			Jsborya.getUserInfo(function (data) {
				vm.userInfo = data;

				vm.getTotal(1);
			});
		},
		methods: {
			getTotal (type) {
				const vm = this;

				var json = {
					'params': {
						'type': type,
					},
					'userInfo': vm.userInfo
				};

				vm.getLatelySixMonth();

				vm.AJAX('/jleas/w/account/businessIncome', json, function (data) {
					vm.totalFee = data.data.totalFee;
					vm.off.type = type;

					vm.monthList.forEach((m, index) => {
						let item = data.data.list[index];
						if (item) {
							vm.monthList[index].totalFee = item.totalFee;
							vm.monthList[index].list = item.list;
						}

					});
					vm.checkEmpty(vm.monthList)
					vm.getList();
				});
			},
			getList: function (closeLoad) {//获取首屏号卡数据
				const vm = this;

				let json = {
					'params': {
						'pageSize': 12,
						'type': vm.off.type,
						'lastId': vm.lastId,
					},
					'userInfo': vm.userInfo
				};

				vm.isHaveMore = false;

				vm.AJAX('/jleas/w/account/businessIncomeList', json, function (data) {
					vm.isPullLoad = true;

					let len = data.data.list.length;
					if (len) {
						vm.lastId = data.data.list[len - 1].ordId;
						vm.screenData(data.data.list);
					} else if (json.params.lastId) {
						vm.isHaveMore = true
					}

				}, closeLoad);
			},
			pullLoad: function (e) {//上拉执行
				e.preventDefault();
				var vm = this, obj = document.getElementById('dataBox');

				if (vm.isPullLoad && obj.scrollHeight <= (obj.scrollTop + obj.offsetHeight) && !vm.isHaveMore) {
					vm.isPullLoad = false;
					vm.getList(true);
				} else {
					vm.isHaveMore = false;
				}
			},
			screenData (list) {
				const vm = this;

				let monthList = vm.monthList;

				list.forEach((item, index) => {
					let m = vm.getDateTime(item.time)[1];

					monthList.forEach((month, _index) => {
						let _m = parseInt(month.name);

						if (_m == m) {
							monthList[_index].list.push(item);
						}
					});

				});
				vm.monthList = monthList;
				vm.checkEmpty(monthList);
			},
			checkEmpty (monthList) {
				let empty = true;

				for (let i in monthList) {
					if (monthList[i].list.length) empty = false;
				}

				this.off.empty = empty;
			},
			typeShift (type) {
				this.lastId = 0;
				this.getTotal(type);
			},
			jumpDetails (type, orderId) {
				if ((type == 1 || type == 4) && this.off.type == 1) {
					Jsborya.pageJump({
						url: `commissionYwIncomeDetails.html?orderId=${orderId}&type=${type}`,
						stepCode: '999',
						depiction: "佣金明细",
					});
				}
			},
			getLatelySixMonth () {
				let i = 0, arr = [],
					t = new Date(),
					y = t.getFullYear(), m = t.getMonth() + 1;

				while (i < 6) {
					let mm = m - i, yy = y;

					mm <= 0 && (mm = 12 - Math.abs(mm), yy = y - 1);
					mm < 10 && (mm = '0' + mm);
					let startTime = `${yy}${mm}`;
					// endTime = mm!=12 ? getUnixTime(`${yy}-${(parseInt(mm)+1)}-1 00:00:00`) : getUnixTime(`${yy}-1-1 00:00:00`);

					arr[i] = {
						name: mm + '月',
						list: [],
						totalFee: 0,
						startTime: startTime,
					}
					i++;
				}
				this.monthList = arr;
			},
		}
	});


});